/**
 * CEAport framework to create open APIs for communications enable applications.
 * https://ceaport.googlecode.com
 *
 * Copyright (c) 2008 by respective authors (see below).
 *
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * Contributors:
 * Michael Weiss (michael.r.weiss@gmail.com)
 * 
 */
package org.ceaport.rest.server;

import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;

public class UserResource extends Resource {

	protected Database db;
	protected String conference;
	protected String user;
	
	public UserResource(Context context, Request request, Response response) {
		super(context, request, response);
		
		// extract the conference database
		db = (Database) context.getAttributes().get("db");
		assert db != null;
		
		// extract the conference number
		conference = (String) request.getAttributes().get("conference");
		assert conference != null;
		
		user = (String) request.getAttributes().get("user");
		assert user != null;
		
		// create only one type of representation
		getVariants().add(new Variant(MediaType.TEXT_PLAIN));
	}
	
	/**
	 * Return a full representation.
	 */
	@Override
	public Representation getRepresentation(Variant variant) {
		Conference c = db.getConference(conference);
		User u = c.getUser(user);
		Representation representation = new StringRepresentation(
			(u != null) ? "[ " + u.getName() + " " + u.getPhone() + " ]" : "NA", MediaType.TEXT_PLAIN);
		return representation;
	}
}
